using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace sforceExplorer
{
	/// <summary>
	/// Summary description for frmDBPicker.
	/// </summary>
	public class frmDBPicker : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TreeView treeView1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnNew;
		public String db;
		private System.Data.SqlClient.SqlConnection oConn;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmDBPicker(System.Data.SqlClient.SqlConnection conn)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			oConn = conn;
			LoadTree();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.treeView1 = new System.Windows.Forms.TreeView();
			this.label1 = new System.Windows.Forms.Label();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnNew = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// treeView1
			// 
			this.treeView1.ImageIndex = -1;
			this.treeView1.Location = new System.Drawing.Point(8, 32);
			this.treeView1.Name = "treeView1";
			this.treeView1.SelectedImageIndex = -1;
			this.treeView1.Size = new System.Drawing.Size(176, 184);
			this.treeView1.TabIndex = 0;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(184, 24);
			this.label1.TabIndex = 1;
			this.label1.Text = "Select Database:";
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(192, 40);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(72, 24);
			this.btnOK.TabIndex = 2;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(192, 72);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(72, 24);
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnNew
			// 
			this.btnNew.Location = new System.Drawing.Point(192, 104);
			this.btnNew.Name = "btnNew";
			this.btnNew.Size = new System.Drawing.Size(72, 24);
			this.btnNew.TabIndex = 2;
			this.btnNew.Text = "New";
			this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
			// 
			// frmDBPicker
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(272, 238);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.treeView1);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnNew);
			this.Name = "frmDBPicker";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Databases";
			this.ResumeLayout(false);

		}
		#endregion

		private void LoadTree() 
		{
			this.treeView1.Nodes.Clear();
			TreeNode root = new TreeNode(oConn.DataSource);
			
			if (oConn.State != System.Data.ConnectionState.Open)
				oConn.Open();

			System.Data.SqlClient.SqlCommand cmd = oConn.CreateCommand();
			cmd.CommandText = "Select * From master..sysdatabases";
			cmd.CommandType = System.Data.CommandType.Text;
			System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
			
			while (dr.Read()) 
			{
				root.Nodes.Add(new TreeNode(dr["name"].ToString()));
			}
			dr.Close();
			this.treeView1.Nodes.Add(root);
			this.treeView1.ExpandAll();
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			if (this.treeView1.SelectedNode.Parent != null) 
			{
				this.db = this.treeView1.SelectedNode.Text;
				this.DialogResult = System.Windows.Forms.DialogResult.OK;
				this.Hide();
			} 
			else 
			{
				System.Windows.Forms.MessageBox.Show("Select a database, then click OK.", "Bad Selection", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
			}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.db = "";
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.Hide();
		}

		private void btnNew_Click(object sender, System.EventArgs e)
		{
			frmGetDBName frm = new frmGetDBName();
			if (frm.ShowDialog(this).Equals(System.Windows.Forms.DialogResult.OK))
			{
				System.Data.SqlClient.SqlCommand cmd = oConn.CreateCommand();
				cmd.CommandType = System.Data.CommandType.Text;
				cmd.CommandText = "CREATE DATABASE " + frm.txtDBName.Text;
				cmd.ExecuteNonQuery();
				this.LoadTree();
			}
		}
	}
}
